package com.nut2014.service;

import com.google.gson.Gson;
import com.nut2014.base.BaseService;
import com.nut2014.entity.Cover;
import com.nut2014.entity.CoverLikeUser;
import com.nut2014.mapper.CoverDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CoverService implements BaseService<Cover> {
    @Autowired(required = false)
    CoverDao coverMapper;
    // CoverMapper coverMapper;


    @Override
    public int add(Cover cover) {
        System.out.println("222" + new Gson().toJson(cover));
        return coverMapper.add(cover);
    }

    @Override
    public int delete(int id) {
        return coverMapper.delete(id);
    }

    @Override
    public int update(Cover cover) {
        return coverMapper.update(cover);
    }

    @Override
    public Cover get(int id) {
        Cover cover = coverMapper.get(id);
        //获取当前专辑点赞总数
        cover.setLikeNumber(coverMapper.getCoverLikeCount(id).getLikeCount());
        return cover;
    }

    @Override
    public List<Cover> getAll() {
        return coverMapper.getAll();
    }

    public List<Cover> getCoverList() {
        return coverMapper.getCoverList();
    }

    public List<Cover> getUserCoverList(int userId) {
        return coverMapper.getUserCoverList(userId);
    }

    public int userIsLike(int userId, int coverId) {
        int size = coverMapper.getUserIsLike(coverId, userId).size();
        System.out.println(size+"???");
        return size;
    }

    public Cover updateLike(int userId, int coverId) {

        //获得当前用户点赞当前专辑的列表
        List<CoverLikeUser> userLikeList = coverMapper.getUserIsLike(coverId, userId);
        if (userLikeList.size() > 0) {
            //删除当前用户点赞
            coverMapper.deleteCoverLikeCount(coverId, userId);
        } else {
            //增加当前用户点赞
            coverMapper.insertLike(userId, coverId);
        }
        //获取当前专辑
        Cover cover = get(coverId);
        //设置当前用户是否点赞
        cover.setLikeCover(userIsLike(userId, coverId));

        //更新当前专辑点赞个数
        Cover likeCover = new Cover();
        likeCover.setId(coverId);
        likeCover.setLikeNumber(cover.getLikeNumber());
        coverMapper.updateLikeCount(likeCover);

        return cover;
    }

}
